package com.lyshop.guiyingshanding.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.lyshop.guiyingshanding.R;
import com.lyshop.guiyingshanding.base.BaseActivity;
import com.lyshop.guiyingshanding.bean.BankListBean;
import com.lyshop.guiyingshanding.bean.RechargeBean;
import com.lyshop.guiyingshanding.bean.UserInfoBean;
import com.lyshop.guiyingshanding.net.ActionCallbackListener;
import com.lyshop.guiyingshanding.utils.CustomerUtil;
import com.lyshop.guiyingshanding.utils.DataUtils;
import com.lyshop.guiyingshanding.utils.RouteUtil;
import com.lyshop.guiyingshanding.utils.TimeCount;
import com.lyshop.guiyingshanding.utils.ToastUtil;
import com.lyshop.guiyingshanding.utils.Utils;
import com.lyshop.guiyingshanding.view.MyEditText;
import com.lyshop.guiyingshanding.view.RechargeDialog;
import com.zc.mychart.utils.FormatUtil;

import java.util.HashMap;
import java.util.Map;

public class RechargeActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_recharge_money, tv_get_code, tv_custom_service;
    private MyEditText met_name, met_id_card_no, met_card_no, met_mobile, met_code;
    private TextView et_bank;
    private Button bt_submit;
    private RelativeLayout rl_bank;
    private BankListBean mBankBean;
    private String mCode;
    private double mRechargeMoney;
    private TimeCount timeCount;
    private RechargeBean mRechargeBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRechargeMoney = getIntent().getExtras().getDouble(RouteUtil.BUNDLE_RECHARGE_MONEY);
        mCode = getIntent().getExtras().getString(RouteUtil.BUNDLE_RECHARGE_CODE);

        initData();
    }

    private void initData() {
        String rechargeMoney = "充值金额<font color=\"#E74C3C\"> " + FormatUtil.formatDouble2(mRechargeMoney) + " </font>元";
        Utils.setHtmlText(tv_recharge_money, rechargeMoney);

        UserInfoBean accountBean = DataUtils.getUserInfo(mContext);
        if(accountBean.auth_flag != 0){
            met_name.setText(accountBean.certname);
            met_id_card_no.setText(accountBean.certno);
            met_name.setEnable(false);
            met_id_card_no.setEnable(false);
        }

        String customString = "充值遇到问题，请<font color=\"#5A92FF\">联系客服</font>";
        Utils.setHtmlText(tv_custom_service, customString);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_recharge;
    }

    @Override
    protected void findViews() {
        tv_recharge_money = findViewById(R.id.tv_recharge_money);
        met_name = findViewById(R.id.met_name);
        met_id_card_no = findViewById(R.id.met_id_card_no);
        rl_bank = findViewById(R.id.rl_bank);
        et_bank = findViewById(R.id.et_bank);
        met_card_no = findViewById(R.id.met_card_no);
        met_mobile = findViewById(R.id.met_mobile);
        tv_get_code = findViewById(R.id.tv_get_code);
        met_code = findViewById(R.id.met_code);
        bt_submit = findViewById(R.id.bt_submit);
        tv_custom_service = findViewById(R.id.tv_custom_service);

    }

    @Override
    protected void setListeners() {
        tv_get_code.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
        rl_bank.setOnClickListener(this);
        tv_custom_service.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == tv_get_code){
            userRecharge();
        }else if (v == bt_submit){
            userRechargeConfirm();
        }else if (v == rl_bank){
            RouteUtil.goBankListActivity(this, RouteUtil.REQUEST_BANK_LIST, mCode);
        }else if (v == tv_custom_service){
            CustomerUtil.customerService(this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && data.getExtras() != null) {
            if (resultCode == Activity.RESULT_OK) {
                if (requestCode == RouteUtil.REQUEST_BANK_LIST) {
                    mBankBean = (BankListBean) data.getExtras().get(RouteUtil.BUNDLE_BANK_MODEL);
                    et_bank.setText(mBankBean.name);
                }
            }
        }
    }

    private void userRecharge() {
        final String mobile = met_mobile.getText().trim();
        final String name = met_name.getText().trim();
        final String idCard = met_id_card_no.getText().trim();
        final String bankCard = met_card_no.getText().trim();

        if (null == mBankBean) {
            ToastUtil.showToast(mContext, "请选择银行");
            return;
        }

        if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(idCard)
                || TextUtils.isEmpty(bankCard) || TextUtils.isEmpty(name)
                || TextUtils.isEmpty(mCode)) {
            ToastUtil.showToast(mContext, "请补全信息后重新提交");
            return;
        }

        loading();

        mAppAction.userSaving(mCode, String.valueOf(mRechargeMoney), bankCard, mobile, name, idCard, mBankBean.bank, new ActionCallbackListener<RechargeBean>() {
                    @Override
                    public void onSuccess(RechargeBean data) {
                        cancelLoading();
                        if (null != data) {
                            timeCount = new TimeCount(tv_get_code, 60000, 1000, "再次获取");
                            timeCount.start();
                            ToastUtil.showToast(mContext, "验证码发送成功");
                            mRechargeBean = data;
                        }
                    }

                    @Override
                    public void onFailure(String errorCode, String message) {
                        cancelLoading();
                        ToastUtil.showToast(mContext, message);
                    }
                });
    }

    private void userRechargeConfirm() {
        String code = met_code.getText();
        if (TextUtils.isEmpty(code)) {
            ToastUtil.showToast(mContext, "请输入验证码");
            return;
        }
        if (null == mRechargeBean) {
            ToastUtil.showToast(mContext, "请先获取验证码");
            return;
        }
        loading();
        mAppAction.userRechargeConfirm(mRechargeBean.bind_card_id, mRechargeBean.order_id, code, new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                cancelLoading();
                //银联支付埋点
                Map<String, Object> userMap = new HashMap<>();
                userMap.put("union_order", mRechargeBean.order_id);
                userMap.put("union_card", mRechargeBean.bind_card_id);
                MobclickAgent.onEventObject(myApplication, "UNION_PAY", userMap);

                RechargeDialog dialog = new RechargeDialog(mContext);
                dialog.init(String.valueOf(mRechargeMoney), new RechargeDialog.DialogListener() {
                    @Override
                    public void onConfirm() {
                        //第一次为绑卡
                        RouteUtil.goMoneyModuleActivity((BaseActivity) mContext, RouteUtil.PAGE_RECHARGE, mRechargeMoney, mCode);
                    }
                });
                dialog.show();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                cancelLoading();
                ToastUtil.showToast(mContext, message);
            }
        });
    }
}